package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.CharCheckUtil;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/26 0026.
 */

public class PartnerRegisterActivity extends BaseActivity implements View.OnClickListener{


    private ImageView partner_register_back;
    private EditText partner_register_phone,partner_register_code;
    private TextView partner_register_getCode,partner_register_config;

    private int countSeconds = 60;// 倒数秒数

    private Handler hanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (countSeconds > 0) {
                        --countSeconds;
                        partner_register_getCode.setText(countSeconds + "s");
                        hanlder.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        countSeconds = 60;
                        partner_register_getCode.setText("重新获取");
                        partner_register_getCode.setClickable(true);
                    }
                    break;
            }
        }
    };
    // 开始倒计时
    private void startCountBack() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                partner_register_getCode.setText(countSeconds + "s");
                hanlder.sendEmptyMessage(0);
                partner_register_getCode.setClickable(false);
            }
        });
    }

    private void getCode(){
        String phone = partner_register_phone.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        XUtil.Post(UrlConfig.GETCODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("获取验证码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    startCountBack();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取验证码失败");
            }
        });
    }

    private void register(){
        String phone = partner_register_phone.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        String code = partner_register_code.getText().toString().trim();
        if (code.isEmpty()){
            showToast("请输入验证码");
            return;
        }
        String pasword = phone.substring(5,phone.length());
        //Log.e("info","pasword="+pasword);
        Map<String,Object> map = new HashMap<>();
        map.put("login_name",phone);
        map.put("verification",code);
        map.put("invite", MyApp.userData.getData().getPhone());
        map.put("login_passwd",pasword);
        showProgressDialog(true);
        XUtil.Post(UrlConfig.REGISTER_URL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("帮好友注册result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    finish();
                }
                showToast(jsonObject.getString("msg"));

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner_register);
        initView();
    }

    private void initView(){
        partner_register_back = (ImageView) findViewById(R.id.partner_register_back);
        partner_register_back.setOnClickListener(this);
        partner_register_phone = (EditText) findViewById(R.id.partner_register_phone);
        partner_register_code = (EditText) findViewById(R.id.partner_register_code);
        partner_register_getCode = (TextView) findViewById(R.id.partner_register_getCode);
        partner_register_getCode.setOnClickListener(this);
        partner_register_config = (TextView) findViewById(R.id.partner_register_config);
        partner_register_config.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.partner_register_back:
                finish();
                break;
            case R.id.partner_register_getCode:
                getCode();
                break;
            case R.id.partner_register_config:
                register();
                break;
        }
    }
}
